// Replication figure b4 in the appendix

// Declare outcome variable
global y dv_nongroup_inc

//Decleare kernel
global kernel uniform

//Declare running variable
global run rv
global run2 rv rv_dem 


/// LOOP
foreach sample in 1 2 3 4 5   {
use "data/table3/fh_fia_inter.dta", replace

// Shorter names
g d=dem
g m=maj
g dm=demmaj

keep if sample`sample'==1

//time dummies
quietly: tab yr, g(dcon)



 rdbwselect $y $run, kernel($kernel) 
	forval i = 5(1)20 {
	scalar h`i'=`i'*e(h_mserd)/10
	g bw`i'=0
	replace bw`i'=1 if $run > -`=h`i'' & $run < `=h`i''
	} 
	
	
	forval i = 5(1)20 {
		reg $y d $run2 if bw`i'==1, rob 
			scalar bA`i'= _b[d]
			scalar seA`i'= _se[d]
		reg $y d m dcon* $run2 if bw`i'==1, rob 
			scalar bD`i'= _b[d]
			scalar seD`i'= _se[d]
		
		matrix a`i' =[h`i',bA`i',seA`i',bD`i',seD`i']

		} 
	
	matrix a=[a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 \	a18 \	a19 \	a20 ]
		
	svmat a
	rename a1 h
	label variable h "Bandwidth"
	
	rename a2 bA
	rename a3 seA
	g upA = bA+1.96*seA
	g lowA =  bA-1.96*seA
	
	rename a4 bD
	rename a5 seD
	g upD = bD+1.96*seD
	g lowD =  bD-1.96*seD
	
	foreach x in bD upD lowD {
	replace `x'=. if seD==0
	}

	
	line  bA upA lowA bD upD lowD h, xline(`=h10')  title(`: variable label sample`sample'', size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black red red red) ///
			lstyle(solid shortdash shortdash solid shortdash shortdash) ///
			yscale(r(-400 200)) ylabel(-400(100)200) ///
			legend(order(1 4) label(1 "Short")  label(4 "Long+year-FE") rows(1)  region(lcolor(white) lwidth(0)))
	

	
	graph save "results/sample`sample'.gph" , replace

	scalar drop _all
	}

	
// graph combine
grc1leg results/sample3.gph results/sample4.gph results/sample5.gph results/sample1.gph results/sample2.gph  ///
 , legendfrom(results/sample1.gph) graphregion(color(white)) rows(1) 
graph display, ysize(6) xsize(18)

// graph export final graph
graph export "results/nongroup.pdf", replace


// erase single graphs
foreach f in results/sample3.gph results/sample4.gph results/sample5.gph results/sample1.gph results/sample2.gph {
	cap erase "`f'"
}

